package com.qa.controller.user;

import com.qa.dto.UserLoginDTO;
import com.qa.dto.UserRegisterDTO;
import com.qa.entity.User;
import com.qa.result.Result;
import com.qa.service.UserService;
import com.qa.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 徐
 */
@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/user/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDTO userLoginDTO) {
        User user = new User();
        // 属性拷贝
        BeanUtils.copyProperties(userLoginDTO, user);

        return userService.login(user);
    }

    @ApiOperation("退出登录")
    @GetMapping("/logout")
    public Result logout() {
        return userService.logout();
    }

    @ApiOperation("测试")
    @GetMapping("/test")
    public Result test() {
        return Result.success(UserUtil.getCurrentUserId());
    }

    @ApiOperation("注册")
    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterDTO userRegisterDTO) {
        return userService.register(userRegisterDTO);
    }

    @ApiOperation("发送验证码")
    @GetMapping("/sendCode/{mail}")
    public Result sendCode(@PathVariable String mail) {
        return userService.sendCode(mail);
    }

    @ApiOperation("修改昵称")
    @PutMapping("/update/{nickname}")
    public Result changeNickname(@PathVariable String nickname) {
        return userService.changeNickname(nickname);
    }
}
